# -*- coding:utf-8 -*-

from flask import Flask
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from config import config


bootstrap = Bootstrap()
db = SQLAlchemy()
migrate = Migrate()
login_manager = LoginManager()
login_manager.session_protection = 'basic'
login_manager.login_view = 'auth.login'
login_manager.login_message = u'请先登录'
carousel = UploadSet('carousel', IMAGES)


def create_app(config_name):
	app = Flask(__name__)
	app.config.from_object(config[config_name])
	config[config_name].init_app(app)

	bootstrap.init_app(app)
	db.init_app(app)
	login_manager.init_app(app)
	migrate.init_app(app, db)

	configure_uploads(app, carousel)
	patch_request_class(app)

	from app.views.home import home as home_blueprint
	app.register_blueprint(home_blueprint, url_prefix='/home')

	from app.views.auth import auth as auth_blueprint
	app.register_blueprint(auth_blueprint, url_prefix='/auth')

	from app.views.business.other import other as other_blueprint
	app.register_blueprint(other_blueprint, url_prefix='/business/other')

	from app.views.business.car import car as car_blueprint
	app.register_blueprint(car_blueprint, url_prefix='/business/car')

	from app.views.account.org import org as org_blueprint
	app.register_blueprint(org_blueprint, url_prefix='/account/org')

	from app.views.account.outside import outside as outside_blueprint
	app.register_blueprint(outside_blueprint, url_prefix='/account/outside')

	from app.views.siteSetting import siteSetting as setting
	app.register_blueprint(setting, url_prefix='/setting')

	from app.views.safe import safe
	app.register_blueprint(safe, url_prefix='/safe')

	from app.views.tracking.assign import assign as assign_blueprint
	app.register_blueprint(assign_blueprint, url_prefix='/tracking/assign')

	from app.views.tracking.track import track as track_blueprint
	app.register_blueprint(track_blueprint, url_prefix='/tracking/track')

	from app.views.personal_center import personal_center as center
	app.register_blueprint(center, url_prefix='/center')

	from app.views.tracking.advice import advice as advice_blueprint
	app.register_blueprint(advice_blueprint, url_prefix='/tracking/advice')

	from app.views.risk.application import application as appli_blueprint
	app.register_blueprint(appli_blueprint, url_prefix='/risk/application')

	from app.views.risk.loan import loan as loan_blueprint
	app.register_blueprint(loan_blueprint, url_prefix='/risk/loan')

	from app.views.risk.loan_track import loan_track as track_blueprint
	app.register_blueprint(track_blueprint, url_prefix='/risk/loan_track')

	from app.views.risk.dispatch import dispatch
	app.register_blueprint(dispatch, url_prefix='/risk/dispatch')

	from app.views.init import initial as initial_blueprint
	app.register_blueprint(initial_blueprint, url_prefix='/initial')

	return app
